﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoGiaitri.Models;
using Newtonsoft.Json;
using PagedList;
namespace VideoGiaitri.Controllers
{
    public class HomeController : Controller
    {
        tinviet_vietnamEntities db = new tinviet_vietnamEntities();
        public ActionResult Index()
        {
            return RedirectToAction("Video");
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }
        public string getRelated(int id) {
            var p = (from q in db.videogiaitris where !q.id.Equals(id) select q).OrderByDescending(o => o.id).Take(10);
            return JsonConvert.SerializeObject(p.ToList());
        }
        public string getOldRelated(int id)
        {
            var p = (from q in db.videogiaitris where q.id<id select q).OrderByDescending(o => o.id).Take(9);
            return JsonConvert.SerializeObject(p.ToList());
        }
        public string getRelatedGame(int id,int typegame)
        {
            var p = (from q in db.topgames where !q.id.Equals(id) && q.typegame==typegame select q).OrderByDescending(o => o.id).Take(10);
            return JsonConvert.SerializeObject(p.ToList());
        }
        public string getOldRelatedGame(int id,int typegame)
        {
            var p = (from q in db.topgames where q.id < id && q.typegame==typegame select q).OrderByDescending(o => o.id).Take(9);
            return JsonConvert.SerializeObject(p.ToList());
        }
        public ActionResult getContent() {
            return View();
        }
        public string getFullContent(string link)
        {
            return Uti.getFullContentMusic(link);
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }
        public ActionResult aoe(int? page)
        {
            ViewBag.title = "Tất cả các clip AOE mới và hay nhất";
            var p = (from q in db.topgames where q.typegame==1 select q).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult AoeXem(int id)
        {
            topgame vd = db.topgames.Find(id);
            if (vd == null)
            {
                return HttpNotFound();
            }
            return View(vd);
        }
        public ActionResult video(int? page)
        {
            ViewBag.title = "Tất cả các Video giải trí hay, mới, lạ";
            var p = (from q in db.videogiaitris select q).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(p.ToPagedList(pageNumber,pageSize));
        }
        public ActionResult Search(int? page,string keyword)
        {
            ViewBag.title = "Tìm kiếm video có chứa từ khóa " + keyword;
            ViewBag.keyword = keyword;
            var p = (from q in db.videogiaitris where q.title.Contains(keyword) select q).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public string getVideoApp(int? page)
        {
            //ViewBag.title = "Tất cả các Video giải trí hay, mới, lạ";
            var p = (from q in db.videogiaitris select new {id=q.id,image=q.image,title=q.title}).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return JsonConvert.SerializeObject(p.ToPagedList(pageNumber, pageSize).ToList());
        }
        public string getLmhtApp(int? page)
        {
            //ViewBag.title = "Tất cả các Video giải trí hay, mới, lạ";
            var p = (from q in db.topgames where q.typegame==2 select new { id = q.id, image = q.image, title = q.title }).OrderByDescending(o => o.id);
            int pageSize = 50;
            int pageNumber = (page ?? 1);
            return JsonConvert.SerializeObject(p.ToPagedList(pageNumber, pageSize).ToList());
        }
        public ActionResult xem(int id)
        {
            videogiaitri vd = db.videogiaitris.Find(id);
            if (vd == null)
            {
                return HttpNotFound();
            }
            return View(vd);
        }
        public ActionResult mXem(int id)
        {
            videogiaitri vd = db.videogiaitris.Find(id);
            if (vd == null)
            {
                return HttpNotFound();
            }
            return View(vd);
        }
        public ActionResult mXemLmht(int id)
        {
            topgame vd = db.topgames.Find(id);
            if (vd == null)
            {
                return HttpNotFound();
            }
            return View(vd);
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
